<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * All function in order.
	 * 1. index
	 * 2. about
	 * 3. contact
	 * 4. games
	 * 5. news
	 * 6. rules
	 * 7. signin
	 * 8.view
	 * 9. success
	 * Non-View functions are below in a seperate comment block.
	 */
	public function index()
	{
		$data['title'] = 'New Paltz HvZ - Home';
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('home_view');
		$this->load->view('includes/footer_view');
	}
	
	public function about()
	{
		$data['title'] = "New Paltz HvZ - About";	
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('about_view');
		$this->load->view('includes/footer_view');
	}
	
	public function contact()
	{
		$data['title'] = "New Paltz HvZ - Contact";	
		$this->load->helper('form');
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('contact_view');
		$this->load->view('includes/footer_view');
	}
	
	public function games(){
			$this->load->model('Site_model');
			$data['title'] = "New Paltz HvZ - Games";
			$data['records'] = $this->db->get('Games');
			$data['current_game'] = $this->db->get_where('Current_Game',array('User_id' => $this->session->userdata('User_id')));
			$this->load->view('includes/header_view',$data);
			$this->load->view('includes/navbar_view');
			$this->load->view('games_view');
			$this->load->view('includes/footer_view');
	}
	
	public function news(){
		$this->load->model('Site_model');
		//Pagination
		$this->load->library("pagination");
		$config['base_url'] = 'home/news/';
		$config['total_rows'] = $this->db->get('News_Feed')->num_rows();
		$config['per_page'] = 10;
		$config['num_links'] = 5;
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div>';
		$config['first_link'] = false;
		$config['last_link'] = false;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev">';
		$config['prev_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['cur_tag_open'] =  '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$this->pagination->initialize($config);
		//////////////////////////////////////////
		$data['records'] = $this->db->get('News_Feed',$config['per_page'],$this->uri->segment(3));
		$data['title'] = "New Paltz HvZ - News Feed";
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('news_view');
		$this->load->view('includes/footer_view');
	}

	
	public function rules()
	{
		$data['title'] = "New Paltz HvZ - Rules";	
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('rules_view');
		$this->load->view('includes/footer_view');
	}
		
	public function signin(){
		$data['title'] = "New Paltz HvZ - Sign In";
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('news_view');
		$this->load->view('includes/footer_view');
		
	}
	

	public function view(){
		if($this->uri->segment(3)){
			$id = $this->uri->segment(3);
			$data['records'] = $this->db->get_where('news_feed',array('id'=>$id));
			$data['title'] = "New Paltz HvZ - Updates";
			$data['id'] = $id;
			$this->load->view('includes/header_view',$data);
			$this->load->view('includes/navbar_view');
			$this->load->view('views_view');
			$this->load->view('includes/footer_view');
		}
		else{
			redirect("http://localhost/hvz/home/");
		}
	}
	
	public function success($message){
		$data['title'] = "New Paltz HvZ - Success";
		$data['message'] = $message();
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('success_view');
		$this->load->view('includes/footer_view');
	}
	
	//added after table of contents was made
	// remember to move them into place and change table.
	
	/*
	*	The complete function is to only be called after a user has been registered.
	*	This will give them information on the site and instructions to finish thier
	*	registration.
	*/
	public function complete(){ //think this through.
		$data['title'] = "New Paltz HvZ - Success";
		$data['message'] = $message();
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('registration_success');
		$this->load->view('includes/footer_view');
	}
	
	/*
	*	All functions for home controller 
	*	1. _news_comment()
	*	2. _randomString($length)
	*	3 ._send_contact()
	*	4. _validate_login()
	*	5. _validate_registration()
	*/
	
	
	function _news_comment(){
		$this->load->library("form_validation");
		$this->load->model('Site_model');
		$this->form_validation->set_rules("comment","Comment","trim|required|alpha_numeric|xss_clean");
		$this->form_validation->set_rules('user','User','trim|required|xss_clean');
		
		if($this->form_validation->run() == FALSE){
			//really need to fix this.
			die("Form not run");
		}
		else{
			//need to change this too way to tired.
			$comment = $this->input->post("comment");
			$user = $this->input->post('user');
			$user = $this->session->userdata('username');
			$post_id = $this->input->post('post_id');
			
			$query = $this->Site_model->_news_comment($user,$comment,$post_id);
			
				redirect('home/updates/'.$post_id);

			
		}
	}
	
	function _randomString($length){
		$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		$string ="";
		for($i=0;$i<$length;$i++)
			$string .= $characters[rand(0, strlen($characters) - 1)];
		return $string;
	}
	
	function _send_contact(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules("name", "Name","trim|required|alpha|xss_clean");
		$this->form_validation->set_rules("email", "Email","trim|required|valid_email|xss_clean");
		$this->form_validation->set_rules("message", "Message","trim|required|alpha_numeric|xss_clean");
		
		if($this->form_validation->run() == FALSE){
			//should probabbly change this to work properly.
			$this->contact();
		}
		else{
			$name = $this->input->post('name');
			$email = $this->input->post('email');
			$message = $this->input->post('message');
			
			$this->load->library('email');
			$this->email->from($email, $email);
			$this->email->to('hvz.sunynewpaltz@gmail.com');
			$this->email->subject("Conatct Request");
			$this->email->message($message);
			if($this->email->send())
				$this->success($message);
			else {
					//again should change this to work properly possibly an ajax request. (not sure).
					$this->contact();
				}
		}
	}
	
	
	function _validate_login(){
		$this->load->library("form_validation");
		$this->form_validation->set_rules("username","Username","trim|required|min_length[6]|max_length[100]|alpha_numeric|xxs_clean");	
		$this->form_validation->set_rules("password","Password","trim|required|min_length[6]|max_length[45]|alpha_numeric|md5|xss_clean");
		
		if($this->form_validation->run() == FALSE){
			$this->sigin();
		}
		else{
			$username = $this->input->post("username");
			$password = $this->input->post("password");
			$remember = $this->input->post("remember_me");
			$this->load->model("Site_model");
			
			if($query = $this->Site_model->_validate_login($username,$password)){
				$user = $this->db->get_where("users",array('username' => $username));
				foreach($user->result() as $row){
					$user_id = $row->id;	
				}
				if($remember == true){
					
					$cookie = array(
					'name' => 'New Paltz HvZ',
					'user_id' => '$user_id',
					'username' => '$username',
					'expire' => '86500',
					'remember' => TRUE,
					'logged_in' => TRUE,
					'secure' => TRUE
					);
					$this->input->set_cookie($cookie);
				}
				else{
					//create the session array
					$session = array(
						'name' => 'New Paltz HvZ',
						'user_id' => '$user_id',
						'username' => '$username',
						'expire' => '86500',
						'logged_in' => TRUE
					);	
					
					$this->session->set_userdata($session);
				}
				redirect('home/index');
			}
			else{
				//redirect back to log in form with an error.
			}
				
		}
	}
	
	function _validate_registration(){ //Need to add email verification to it.
		$this->load->library("form_validation");
		$this->form_validation->set_rules("username","Username","trim|required|min_length[6]|max_length[100]|alpha_numeric|is_unique[users.username]xss_clean");
		$this->form_validation->set_rules("password", "Password","trim|required|min_length[6]|max_length[45]|alpha_numeric|md5|xss_clean");
		$this->form_validation->set_rules("repeat_password", "Repeat Password","trim|required|min_length[6]|max_length[32]|alpha_numeric|matches(password)|xss_clean");
		$this->form_validation->set_rules("first_name", "First Name","trim|required|alpha|xss_clean");
		$this->form_validation->set_rules("last_name", "Last Name","trim|required|alpha|xss_clean");
		$this->form_validation->set_rules("email", "Email","trim|required|validate_email|xss_clean");
		
		if($this->form_validation->run() == FALSE){
			//should proabably change this as well.
			$this->register();
		}
		else{
			$this->load->model("Register_model");
			
			$username = $this->post("username");
			$password = $this->post("password");
			$repeat_password = $this->post("repeat_password");
			
			$first_name = $this->post("first_name");
			$last_name = $this->post("first_name");
			$email = $this->post("email");
			$remember = $this->post("remember_me");
			$priority = 1;	
			$verification_code = randomString(20);
				if($this->Register_model->_newuser($username,$password,$first_name,$last_name,$email,$priority,$verification_code)){
					$kill_code = randomString(8);
					if($this->Register_model->_insertCurrentGame($username,$kill_code)){
						if($this->Regsiter_model->_createLeaderboardEntry($username)){
							//Should probably redirect to log in screen or validation page.
							redirect("home/complete");
						}else{
							//These errors are piling up what are gonna do about it.	
						}
					}else{
						//Another error soon enough.	
					}
				}else{
					//there is gonna be an error. I knows it.
				}
			
		}
	}
	/*
		These functions were addedd after the table of contents was made remember
		to change the format of the document.
	*/
	
	public function _isLoggedIn(){
		if($this->input->cookie('logged_in',TRUE) == TRUE){
			return true;
		}
		elseif($this->session->userdata('logged_in') == TRUE){
			return true;
		}else{
			return false;
		}
	}
	
	public function getGameId(){
		if($this->input->cookie('user_id',TRUE) == TRUE){
			$user_id = $this->input->cookie('user_id',TRUE);
			if($user = $this->db->get_where("current_game",array('user_id' => $user_id))){
				foreach($user->result() as $row){
					$game_id = $row->game_id;	
				}
			return $game_id;
			}
		}
		elseif($this->session->userdata('user_id') == TRUE){
			$user_id = $this->session->userdata('user_id');
			if($user = $this->db->get_where("current_game",array('user_id' => $user_id))){
				foreach($user->result() as $row){
					$game_id = $row->game_id;	
				}
			return $game_id;
			}
		}
	}
	
	public function getGameId($user_id){
		if($user = $this->db->get_where("current_game",array('user_id' => $user_id))){
			foreach($user->result() as $row){
				$game_id = $row->game_id;	
			}
			return $game_id;
		}
	}
	
	
}
/* End of file home.php */
/* Location: ./application/controllers/home.php*/ 